package com.mycompany.oj.Mapper.Impl;

import com.mycompany.oj.Mapper.StudentMapper;
import com.mycompany.oj.entity.studentInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 *
 * @author asus
 */
@Component
public class MyStudentMapperImpl implements StudentMapper {

    @Autowired
    SqlOperator sqlOperator;

    //获取学生信息
    @Override
    public List<Map<String, Object>> get_student_information(String id) {
        String stmt = "SELECT  nickname,student_name,schoolid,grade,major,sex,school FROM Student where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //修改手机号
    @Override
    public int changePhonenumber(String username, String id) {
        String stmt = "UPDATE Account SET passwd = ? WHERE id = ?;";
        int temp = sqlOperator.update(stmt, username, id);
        return temp;
    }

    //修改密码
    @Override
    public int changePassword(String password, String id) {
        String stmt = "UPDATE Account SET passwd = ? WHERE id = ?;";
        int temp = sqlOperator.update(stmt, password, id);
        return temp;
    }

    //获取做过的题目
    @Override
    public List<Map<String, Object>> getPostNumber(String id) {
        String stmt = "select number from Student_post_number where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //获取通过的题目
    @Override
    public List<Map<String, Object>> getPassNumber(String id) {
        String stmt = "select number from Student_pass_number where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //获取今日计划
    @Override
    public List<Map<String, Object>> getTodayNumber(String id) {
        String stmt = "SELECT number from Everydaynumber where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //获取做题目标
    @Override
    public List<Map<String, Object>> getPlanNumber(String id) {
        String stmt = "SELECT plan_num,nickname,schoolid from Student WHERE Account_id = ? ";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    public List<Map<String, Object>> getLastAddress(String student_id) {
        String stmt = "Select question_name from Question_last_addrress where Account_id= ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, student_id);
        return temp;
    }

    //修改计划
    @Override
    public int updatePlanNumber(String username, String number) {
        String stmt = "UPDATE Student,Account SET plan_num = ? WHERE Account_id = Account.id AND username = ?";
        int temp = sqlOperator.update(stmt, number, username);
        return temp;
    }

    //获取哪个人什么难度的题目完成了几道
    @Override
    public List<Map<String, Object>> finishQuestionNumber(String username, String kind) {
        String stmt = "Select question_num from Student_Pass_kind where Account_id = ? and Kind_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, kind);
        return temp;
    }

    //获取哪个人什么难度的题目做了几道
    @Override
    public List<Map<String, Object>> questionNumber(String username, String kind) {
        String stmt = "Select question_num from Student_Post_kind where Account_id = ? and Kind_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username, kind);
        return temp;
    }

    //获取收藏夹内容
    @Override
    public List<Map<String, Object>> getFavorites(String username) {
        String stmt = "Select question_name,Question_id from Get_favorites where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //获取通过的题目
    @Override
    public List<Map<String, Object>> getTrueQuestion(String username) {
        String stmt = "Select Question_id from True_question where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //获取未通过的题目
    @Override
    public List<Map<String, Object>> getFalseQuestion(String username) {
        String stmt = "Select Question_id from False_question where Account_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //删除题目
    public int deleteQuestion(String username, String question_id) {
        String stmt = "DELETE from StudentFavorites where Question_id = ? and Student_id =  ?";
        int temp = sqlOperator.delete(stmt, question_id, username);
        return temp;
    }

    @Override
    public List<Map<String, Object>> getDifficultNumber(String kind) {
        String stmt = "Select num from question_difficult_num where Kind_id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, kind);
        return temp;
    }

    //修改学生信息
    @Override
    public int changeStudentInformation(studentInformation student_information, String username) {
        String stmt = "Update Student set nickname = ?,schoolid=?,grade=?,major=?,sex=?,school=?,student_name = ? where Account_id = ?";
        int temp = sqlOperator.update(stmt, student_information.getNickname(), student_information.getSchoolid(), student_information.getGrade(), student_information.getMajor(), student_information.getSex(), student_information.getSchool(),student_information.getStudent_name(), username);
        return temp;
    }

    //获取最近5题记录
    @Override
    public List<Map<String, Object>> getFiveQuestion(String username) {
        String stmt = "Select * from Five_question where acid = ? LIMIT 5";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //获取各个难度题数
    public List<Map<String, Object>> getDifficultNumber() {
        String stmt = "Select * from questionNumber";
        List<Map<String, Object>> temp = sqlOperator.query(stmt);
        return temp;
    }


    //获取收藏夹
    @Override
    public List<Map<String, Object>> getFavoritesJia(String username) {
        String stmt = "Select favorites_name from My_favorites where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }

    //创建做题记录
    @Override
    public int insertGoGoGo(String student_id, String question_id) {
        String stmt = "Insert into Answer_register(Student_id,Question_id,question_result,question_specific_result,question_time,running_time,memory,result) value(?,?,?,?,now(),?,?,?)";
        String[] temp = sqlOperator.insert(stmt, student_id, question_id, "compiling", "0", "0", "0", "0");
        return Integer.valueOf(temp[0]);
    }

    @Override
    public int insertGoGoGoCourse(String student_id, String question_id) {
        String stmt = "Insert into Question_course_register(Student_id,Question_course_id,question_result,question_specific_result,question_time,running_time,memory,result) value(?,?,?,?,now(),?,?,?)";
        String[] temp = sqlOperator.insert(stmt, student_id, question_id, "compiling", "0", "0", "0", "0");
        return Integer.valueOf(temp[0]);
    }

    public int UpdateCourseResult(String register_id) {
        String stmt = "Update Question_course_register set question_result = 'AC_RIGHT' where id = ?";
        int temp = sqlOperator.update(stmt, register_id);
        return temp;
    }

    //获取做题时间，空间，结果
    @Override
    public List<Map<String, Object>> getTimeMemory(String id) {
        String stmt = "Select question_result,running_time,memory from Answer_register where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }

    //
    @Override
    public List<Map<String, Object>> getTimeMemoryCourse(String id) {
        String stmt = "Select question_result,running_time,memory from Question_course_register where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, id);
        return temp;
    }


    @Override
    public List<Map<String, Object>> getCourseId(String name) {
        String stmt = "Select id from Question_course where question_name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, name);
        return temp;
    }

    @Override
    public List<Map<String, Object>> getQuestionId(String name) {
        String stmt = "Select id from Question where question_name = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, name);
        return temp;
    }

    public List<Map<String, Object>> getResult() {
        String stmt = "Select * from Get_result ";
        List<Map<String, Object>> temp = sqlOperator.query(stmt);
        return temp;
    }

    //用户最后提交时间
    public List<Map<String, Object>> getLastPostTime(String account_id) {
        String stmt = "Select time from last_time where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, account_id);
        return temp;
    }

    //用户最近7天
    public List<Map<String, Object>> lastSevenday(String account_id) {
        String stmt = "Select number from seven_day where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, account_id);
        return temp;
    }

    //上传答案
    public String[] postAnswer(String Question_id, String Student_id, String answer, String language) {
        String stmt = "Insert into Student_answer(Question_id,Student_id,answer,language) value(?,?,?,?)";
        String[] temp = sqlOperator.insert(stmt, Question_id, Student_id, answer, language);
        return temp;
    }

    //获取结果
    public List<Map<String, Object>> getSpecifyResult(String register_id) {
        String stmt = "Select question_result from Question_course_register where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, register_id);
        return temp;
    }

    public List<Map<String, Object>> getAnSpecifyResult(String register_id) {
        String stmt = "Select question_result from Answer_register where id = ?";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, register_id);
        return temp;
    }

    public List<Map<String, Object>> getStudentId(String username) {
        String stmt = "Select Student.id from Student,Account where Account.username = ? AND Student.Account_id = Account.id";
        List<Map<String, Object>> temp = sqlOperator.query(stmt, username);
        return temp;
    }
}
